package net.betou.cms.action.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;
import static net.betou.common.web.freemarker.DirectiveUtils.OUT_BEAN;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.betou.cms.entity.assist.CmsVoteTopic;
import net.betou.cms.entity.main.CmsSite;
import net.betou.cms.manager.assist.CmsVoteTopicMng;
import net.betou.cms.web.FrontUtils;
import net.betou.common.web.freemarker.DirectiveUtils;

import org.springframework.beans.factory.annotation.Autowired;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 投票标签
 */
public class CmsVoteDirective implements TemplateDirectiveModel{
	/**
	 * 输入参数，投票ID。可以为空，为空则获取站点的默认投票。
	 */
	public static final String PARAM_ID = "id";
	/**
	 * 输入参数，站点ID。默认为当前站点。
	 */
	public static final String PARAM_SITE_ID = "siteId";

	@SuppressWarnings( "unchecked" )
	public void execute( Environment env, Map params, TemplateModel [ ] loopVars, TemplateDirectiveBody body ) throws TemplateException, IOException {
		CmsSite site = FrontUtils.getSite( env );
		CmsVoteTopic vote;
		Integer id = getId( params );
		if( id != null ){
			vote = cmsVoteTopicMng.findById( id );
		}else{
			Integer siteId = getSiteId( params );
			if( siteId == null ){
				siteId = site.getId( );
			}
			vote = cmsVoteTopicMng.getDefTopic( siteId );
		}

		Map< String, TemplateModel > paramWrap = new HashMap< String, TemplateModel >( params );
		paramWrap.put( OUT_BEAN, DEFAULT_WRAPPER.wrap( vote ) );
		Map< String, TemplateModel > origMap = DirectiveUtils.addParamsToVariable( env, paramWrap );
		body.render( env.getOut( ) );
		DirectiveUtils.removeParamsFromVariable( env, paramWrap, origMap );
	}

	private Integer getId( Map< String, TemplateModel > params ) throws TemplateException {
		return DirectiveUtils.getInt( PARAM_ID, params );
	}

	private Integer getSiteId( Map< String, TemplateModel > params ) throws TemplateException {
		return DirectiveUtils.getInt( PARAM_SITE_ID, params );
	}

	@Autowired
	private CmsVoteTopicMng cmsVoteTopicMng;
}
